const utils = require('../utils/dataTransform')
// 导入UDP模块
const dgram = require('dgram')
// 发送UDP报文
function sendMessage(HOST='127.0.0.1' , PORT= 80 , message) {
    const buffer = Buffer.from(message);
    utils.printBuffer(buffer)
    console.log('接收地址:' + HOST)
    console.log('接收端口:' + PORT)

    // 创建 UDP 客户端
    const client = dgram.createSocket('udp4');
    //
    client.bind(32100);
    // 监听错误事件
    client.on('error', (err) => {
        console.log(`客户端错误： ${err.stack}`);
        client.close();
    });

    // 发送消息
    client.send(buffer , PORT, HOST, (err) => {
        if (err) throw err;
        console.log('消息已发送');
        client.close();
    });
}
module.exports = {
    sendMessage
}
